package interfaz;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Dimensiones extends JFrame{
	private static final long serialVersionUID = 1L;
	private JPanel mainPanel;
	private JLabel numColumnas;
	private JLabel numFilas;
	private JTextField campoNumColumnas;
	private JTextField campoNumFilas;
	private JButton atrasButton;
	private JButton aceptarButton;
	
	public Dimensiones() {
		inicializaComponentes();
		pack();
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = getSize();
        setLocation(new Point((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.width) / 2));
	}

	private void inicializaComponentes() {
		GridBagConstraints gridBagConstraints;
		
		mainPanel = new JPanel();
		numColumnas = new JLabel();
		numFilas = new JLabel();
		campoNumColumnas = new JTextField();
		campoNumFilas = new JTextField();
		atrasButton = new JButton();
		aceptarButton = new JButton();
		
		setTitle("Dimensiones nuevo tablero");
		
		mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
	    mainPanel.setMinimumSize(new Dimension(297, 200));
	    mainPanel.setLayout(new GridBagLayout());
	    
	    numColumnas.setText("Columnas:");
	    gridBagConstraints = new java.awt.GridBagConstraints();
	    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
	    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
	    gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
	    mainPanel.add(numColumnas, gridBagConstraints);
	    
	    campoNumColumnas.setColumns(5);
	    campoNumColumnas.setEditable(true);
	    gridBagConstraints = new java.awt.GridBagConstraints();
	    gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
	    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
	    gridBagConstraints.weightx = 1.0;
	    gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
	    mainPanel.add(campoNumColumnas, gridBagConstraints);
	    
	    numFilas.setText("Filas:");
	    gridBagConstraints = new java.awt.GridBagConstraints();
	    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
	    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
	    gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
	    mainPanel.add(numFilas, gridBagConstraints);
	    
	    campoNumFilas.setColumns(5);
	    campoNumFilas.setEditable(true);
	    gridBagConstraints = new java.awt.GridBagConstraints();
	    gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
	    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
	    gridBagConstraints.weightx = 1.0;
	    gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
	    mainPanel.add(campoNumFilas, gridBagConstraints);
	    
	    aceptarButton.setMnemonic('A');
        aceptarButton.setText("Aceptar");
        aceptarButton.setToolTipText("Aceptar");
        aceptarButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
            	aceptarButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridwidth = GridBagConstraints.RELATIVE;
        gridBagConstraints.gridheight = GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = GridBagConstraints.SOUTH;
        gridBagConstraints.weighty = 1.0;
        mainPanel.add(aceptarButton, gridBagConstraints);
        
	    atrasButton.setMnemonic('V');
        atrasButton.setText("Volver");
        atrasButton.setToolTipText("Volver");
        atrasButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                atrasButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = GridBagConstraints.SOUTH;
        gridBagConstraints.weighty = 1.0;
        mainPanel.add(atrasButton, gridBagConstraints);
        
        getContentPane().add(mainPanel, BorderLayout.CENTER);
	}

	protected void aceptarButtonActionPerformed(ActionEvent evt) {
		
		this.setVisible(false);
	}

	protected void atrasButtonActionPerformed(ActionEvent evt) {
		new NuevaPartida().setVisible(true);
		this.setVisible(false);	
	}

}
